//
//  ST_RechargeSweetCoinCell.swift
//  btvideo
//
//  Created by mac on 2020/7/1.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_RechargeSweetCoinCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        self.contentView.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        
        self.contentView.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 10, left: 15, bottom: 0, right: 15))
        }
        
        
        root_view.addSubview(leftView)
        leftView.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo((ScreenWidth - 30)/345.0*150)
        }
        
        root_view.addSubview(rightView)
        rightView.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(leftView.snp.right)
        }
        
        
        
        leftView.addSubview(number_label)
        number_label.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        rightView.addSubview(current_price_label)
        current_price_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(36)
            make.top.equalToSuperview().offset(17)
        }
        
        rightView.addSubview(original_price_label)
        original_price_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(17)
            make.top.equalTo(current_price_label.snp.bottom).offset(3)
        }
        
        original_price_label.addSubview(redLine)
        redLine.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(-7)
            make.right.equalToSuperview().offset(7)
            make.height.equalTo(1)
        }
        
        self.addSubview(bubble_view)
        bubble_view.snp.makeConstraints { (make) in
            make.centerX.equalTo(rightView)
            make.top.equalToSuperview()
            make.size.equalTo(CGSize.zero)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //    MARK:- Properties
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.cornerRadius = 10
        control.backgroundColor = .white
        return control
    }()
    
    fileprivate lazy var leftView:UIView = {
       let control = UIView()
        return control
    }()
    
    fileprivate lazy var rightView:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.white
        return control
    }()
    

    fileprivate lazy var number_label:UILabel = {
        let control = UILabel.init()
        return control
    }()
    
    fileprivate lazy var current_price_label:UILabel = {
        let control = UILabel.init()
        return control
    }()
    
    fileprivate lazy var original_price_label:UILabel = {
        let control = UILabel.init()
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        control.textColor = UIColor.init(hex: 0xED3E58)
        return control
    }()
    
    fileprivate lazy var redLine:UIView = {
        let control = UIView()
        control.isHidden = true
        control.backgroundColor = UIColor.init(hex: 0xED3E58)
        return control
    }()
    
    fileprivate lazy var bubble_view:ST_BubbleView = {
        let control = ST_BubbleView()
        control.isHidden = true
        return control
    }()
    
    
    
//    MARK: - Bind Model
    var model = ST_RechargeSweetCoinCellModel(){
        didSet{
            
            leftView.backgroundColor = model.leftColor
            
            if let color = model.borderColor{
                self.root_view.borderColor = color
                self.root_view.borderWidth = 3
            }else{
                self.root_view.borderColor = .clear
            }
            
            number_label.attributedText = model.coinNumberAttrStr
            
            
            current_price_label.attributedText = model.priceAttrStr
            
            original_price_label.text = model.thrift
            
//            redLine.isHidden = model.hiddenOriginal
            
            
            if model.detail_desc.count > 0{
                bubble_view.text = model.detail_desc
                bubble_view.isHidden = false
                bubble_view.snp.updateConstraints { (make) in
                    make.size.equalTo(model.bubbleViewSize)
                }
            }else{
                bubble_view.isHidden = true
            }
            
            
        }
        
    }
}


//MARK: -
class ST_RechargeSweetCoinCenterCell: ST_RechargeSweetCoinCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        current_price_label.snp.remakeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



//MARK: - Model
class ST_RechargeSweetCoinCellModel: BTMemberGoodsModel {
    
    var leftColor = UIColor.init(hex: 0xACB4C0)!
    
    var borderColor:UIColor?
    
    var topSignText:String?
    
    
    var coinNumberAttrStr = NSAttributedString()
    
    var priceAttrStr = NSAttributedString()
    
    var originalPriceStr = ""
    
    var hiddenOriginal = false
    
    var bubbleViewSize = CGSize.zero
    
    func setUpData(idx:Int){
        
        let coinNumberStr = NSMutableAttributedString.init(string: self.limited_time.string)
        
        coinNumberStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font:UIFont.init(name: "DINPro-Black", size: 28)!], range: NSRange.init(location: 0, length: coinNumberStr.length))
        
        let unitStr = NSMutableAttributedString.init(string: " 个")
        
        unitStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.baselineOffset:1,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)], range: NSRange.init(location: 0, length: unitStr.length))
        
        coinNumberStr.append(unitStr)
        
        coinNumberAttrStr = coinNumberStr
        
        
        let priceStr = NSMutableAttributedString.init(string: self.original_price.int.string)
        
        priceStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.init(hex: 0xED3E58)!,NSAttributedString.Key.font:UIFont.init(name: "DINPro-Black", size: 28)!], range: NSRange.init(location: 0, length: priceStr.length))
        
        let priceUnitStr = NSMutableAttributedString.init(string: " 元")
        
        priceUnitStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.init(hex: 0x61666F)!,NSAttributedString.Key.baselineOffset:1,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)], range: NSRange.init(location: 0, length: priceUnitStr.length))
        
        priceStr.append(priceUnitStr)
        
        priceAttrStr = priceStr
       
        if let c = UIColor.init(hexString: self.color){
            self.leftColor = c
        }
        
        
        if self.border == 1{
            self.borderColor = self.leftColor
        }
        
        if self.detail_desc.count > 0{
            
            let w = self.detail_desc.width(withConstrainedHeight: 100, font: UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium)) + 21
            
            self.bubbleViewSize = CGSize.init(width: w, height: 22+5)
        }
        
        
        
    }
    
}


//MARK: - Bubble View
fileprivate class ST_BubbleView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(bottomImageView)
        bottomImageView.snp.makeConstraints { (make) in
            make.width.equalTo(10)
            make.height.equalTo(5)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 0, bottom: 5, right: 0))
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    fileprivate lazy var bottomImageView:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("倒三角形_icon"))
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.backgroundColor = Color.init(hexString: "#ED3E58")
        control.textColor = .white
        control.textAlignment = .center
        control.cornerRadius = 11
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium)
        return control
    }()
    
    var text = String(){
        didSet{
            content_label.text = text
        }
    }
    
}
